package entity;


import map.Map;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import animation.MacAnimation;
import constans.Parametr;
import constans.ResourceManager;

public class Unit extends MovingEntity{
	
	boolean canMove = false;
	float range = Parametr.URange;

	float attackSpeed;
	float attackTime = 0;
	boolean canAttack = false;	
	float attackAccuracy;
	
	boolean animate = false;
	
	private UnitCannon cannon;
	private boolean renderLifeBar = false;
	
	public Unit(Vector2f position) {
		super(position);
		size = 40;
		speed = Parametr.USpeed + (float)Math.random() * Parametr.USpeedDX;
		maxLife = Parametr.UMaxLife += Parametr.UMaxLifeDX;
		damage = Parametr.UDamage += Parametr.UDmageDX;
		actLife = maxLife;
		range = Parametr.URange + (float)Math.random() * Parametr.URangeDX;
		regenerationSpeed = Parametr.URegenerationSpeed;
		animation = new MacAnimation(ResourceManager.getImage("robot"), 100, size);
		
		attackSpeed = Parametr.UAttackSpeed;
		attackAccuracy = Parametr.UAttackAccuracy;
		
		cannon = new UnitCannon(position);
		cannon.update(damage, attackSpeed, attackAccuracy);
	}
	public void render(GameContainer container, Graphics g) throws SlickException
	{	
		position.sub(Map.shift);
		animation.draw(position.x - size/2, position.y - size/2);
		if(renderLifeBar && !isDead())
			renderLifeBar(container, g);
		position.add(Map.shift);
		cannon.render(container, g);
	}
	public void renderLifeBar(GameContainer container, Graphics g) throws SlickException
	{	
		g.setColor(Color.black);
		g.fillRoundRect(position.x - size/2, position.y - size/2 - 10, size, 10, 3);
		g.setColor(new Color(0,70,0));
		g.fillRoundRect(position.x - size/2, position.y - size/2 - 10, actLife/maxLife * size, 10, 3);
	}
	public void update(GameContainer container, int delta) throws SlickException
	{		
		if(target.distance(position) < range)
		{
			cannon.isInRange = true;
		}
		else
		{
			cannon.isInRange = false;
			move();
			animation.setDirection(movement);
			animation.update();
		}
		cannon.setDirection(new Vector2f(target));
		
		cannon.update(container, delta);
		
		Input input = container.getInput();
		Vector2f mousePosition = new Vector2f(input.getMouseX(), input.getMouseY());
		if(isOn(mousePosition.add(Map.shift)))
			renderLifeBar = true;
		else
			renderLifeBar = false;
		
		//lifeRegeneration();
	}
	public void setTarget(Vector2f t)
	{
		target.set(t);
	}
	public void setCanMove(boolean s)
	{
		canMove = s;
	}
	//shooting	
	public Bullet shoot()
	{
		cannon.shoot = false;
		return cannon.shoot();
	}
	public boolean isBullet() {
		
		return cannon.shoot;
	}

}
